package pl.elka.simulator.core.configurationCommand.impl;

import java.util.List;

import pl.elka.simulator.core.GravityPoint;
import pl.elka.simulator.core.Node;
import pl.elka.simulator.core.Position;
import pl.elka.simulator.core.configurationCommand.IConfigurationCommand;
import pl.elka.simulator.core.configurationCommand.impl.param.PositionCommandParam;
import pl.elka.simulator.core.mobilityManager.impl.nodes.LeaderNode;

public class CommandUtils {
	public static Position getPosition(IConfigurationCommand cmd) {
		return ((PositionCommandParam)cmd.getParam()).getPosition();
	}
	
	public static LeaderNode finLeaderNodeByPosition(List<LeaderNode> nodesToSearch, Position position) {		
		float px = position.getX();
		float py = position.getY();
		for(LeaderNode n:nodesToSearch){
			float r = n.getRadius();
			float nx = n.getPosition().getX();
			float ny = n.getPosition().getY();
			
			if(nx-r<px && nx+r>px && ny+r>py && ny-r<py){
				//TODO mozna doimplementowac dokladniejszy warunek
				return n;
			}
		}
		return null;
	}
	
	public static Node finNodeByPosition(List<? extends Node> nodesToSearch, Position position) {		
		float px = position.getX();
		float py = position.getY();
		for(Node n:nodesToSearch){
			float r = n.getRadius();
			float nx = n.getPosition().getX();
			float ny = n.getPosition().getY();
			
			if(nx-r<px && nx+r>px && ny+r>py && ny-r<py){
				//TODO mozna doimplementowac dokladniejszy warunek
				return n;
			}
		}
		return null;
	}

	public static GravityPoint finGravityPointByPosition(List<GravityPoint> points, Position position) {
		float px = position.getX();
		float py = position.getY();
		for(GravityPoint n:points){
			float r = n.getRadius();
			float nx = n.getPosition().getX();
			float ny = n.getPosition().getY();
			
			if(nx-r<px && nx+r>px && ny+r>py && ny-r<py){
				//TODO mozna doimplementowac dokladniejszy warunek
				return n;
			}
		}
		return null;
	}
}
